package com.xsy.android.compose.we.component.ui.page.chat

import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.drawWithContent
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import com.xsy.android.compose.we.component.data.Chat
import com.xsy.android.compose.we.component.ui.compose.WeTopBar
import com.xsy.android.compose.we.component.ui.theme.WeComposeTheme
import com.xsy.android.compose.we.component.view_model.WeViewModel

/**
 *@author: xiaosy
 *时间: 2024/6/27
 *@Description: java类作用描述
 *@Version: 1.0
 **/
@Composable
fun ChatList(chats: List<Chat>, navController: NavHostController) {
    Column(
        Modifier
            .background(WeComposeTheme.colors.background)
            .fillMaxSize()
    ) {
        WeTopBar(title = "微信")
        LazyColumn(Modifier.background(WeComposeTheme.colors.listItem)) {
            itemsIndexed(chats) { index, chat ->
                ChatListItem(chat = chat, index = index, navController = navController)
                if (index < chats.lastIndex) {
                    HorizontalDivider(
                        modifier = Modifier.padding(start = 68.dp),
                        color = WeComposeTheme.colors.chatListDivider,
                        thickness = 0.8f.dp
                    )
                }
            }
        }
    }
}

@Composable
private fun ChatListItem(chat: Chat, index: Int, navController: NavHostController) {
    Row(
        Modifier
            .clickable {
                navController.navigate("chat_page/$index")
            }
            .fillMaxWidth()
    ) {
        Image(
            painter = painterResource(id = chat.friend.avatar),
            contentDescription = null,
            modifier = Modifier
                .padding(8.dp)
                .size(48.dp)
                .unread(!chat.msgs.last().read, WeComposeTheme.colors.badge)
                .clip(RoundedCornerShape(4.dp))
        )
        Column(
            Modifier
                .weight(1f)
                .align(Alignment.CenterVertically)
        ) {
            Text(
                text = chat.friend.name,
                fontSize = 18.sp,
                color = WeComposeTheme.colors.textPrimary
            )
            Text(
                text = chat.msgs.last().text,
                fontSize = 14.sp,
                color = WeComposeTheme.colors.textSecondary
            )
        }
        Text(
            text = chat.msgs.last().time,
            Modifier.padding(8.dp, 8.dp, 12.dp, 8.dp),
            fontSize = 12.sp,
            color = WeComposeTheme.colors.textSecondary
        )
    }

}

fun Modifier.unread(show: Boolean, color: Color): Modifier = this.drawWithContent {
    drawContent()
    if (show) {
        drawCircle(color, 6.dp.toPx(), Offset(size.width - 1.dp.toPx(), 1.dp.toPx()))
    }
}

@Preview(showBackground = true)
@Composable
fun PreviewChatList() {
    val viewModel: WeViewModel = viewModel()
    val navController = rememberNavController()
    ChatList(viewModel.chats, navController)
}